package com.ruoyi.project.poem.poem.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.poem.poem.domain.Poem;
import com.ruoyi.project.poem.poem.service.IPoemService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 古诗词Controller
 * 
 * @author shixiangwen
 * @date 2020-03-30
 */
@Controller
@RequestMapping("/poem/poem")
public class PoemController extends BaseController {
	private String prefix = "poem/poem";

	@Autowired
	private IPoemService poemService;

	@RequiresPermissions("poem:poem:view")
	@GetMapping()
	public String poem() {
		return prefix + "/poem";
	}

	/**
	 * 查询古诗词列表
	 */
	@RequiresPermissions("poem:poem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Poem poem) {
		startPage();
		List<Poem> list = poemService.selectPoemList(poem);
		return getDataTable(list);
	}

	/**
	 * 导出古诗词列表
	 */
	@RequiresPermissions("poem:poem:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Poem poem) {
		List<Poem> list = poemService.selectPoemList(poem);
		ExcelUtil<Poem> util = new ExcelUtil<Poem>(Poem.class);
		return util.exportExcel(list, "poem");
	}

	/**
	 * 新增古诗词
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存古诗词
	 */
	@RequiresPermissions("poem:poem:add")
	@Log(title = "古诗词", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Poem poem) {
		return toAjax(poemService.insertPoem(poem));
	}

	/**
	 * 修改古诗词
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		Poem poem = poemService.selectPoemById(id);
		mmap.put("poem", poem);
		return prefix + "/edit";
	}

	/**
	 * 修改保存古诗词
	 */
	@RequiresPermissions("poem:poem:edit")
	@Log(title = "古诗词", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Poem poem) {
		return toAjax(poemService.updatePoem(poem));
	}

	/**
	 * 删除古诗词
	 */
	@RequiresPermissions("poem:poem:remove")
	@Log(title = "古诗词", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(poemService.deletePoemByIds(ids));
	}
}
